package com.jqr.activity.listviewdemo.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jqr.activity.listviewdemo.R;
import com.jqr.activity.listviewdemo.model.Article;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<Article> articleList;
    private LayoutInflater layoutInflater;
    private Context context;
    public MyAdapter(Context context,List<Article> articleList){
        this.context=context;
        this.articleList=articleList;
        layoutInflater=LayoutInflater.from(context);

    }
    @Override
    public int getCount() {
        return articleList.size();
    }

    @Override
    public Object getItem(int position) {
        return articleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return articleList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView=  layoutInflater.inflate(R.layout.list_item,parent,false);
        }
        ImageView imageView=convertView.findViewById(R.id.ivImg);
        TextView tvTitle=convertView.findViewById(R.id.tvTitle);
        TextView tvContent=convertView.findViewById(R.id.tvContent);
        Article article=articleList.get(position);
        imageView.setImageResource(article.getImgId());
        tvTitle.setText(article.getTitle());
        tvContent.setText(article.getIntr());
        return convertView;
    }
}
